<?php

require_once './include/common.inc.php';
checkLogin();

$page_title = 'jans2en furniture - Inventory - Product Details';
$menu_id = 6;
getMyParam('categoryid');
getMyParam('inventoryid');
getMyParam('pid');
getMyParam('fid');
getMyParam('gotype');
getMyParam('names');
$fid = (int)$fid;


$cate_id = $fid>0 ? $fid : $categoryid;
//===========================check if the category is published=====//
$showProductCategory = true;  //the category of the product
if($fid >0 && $categoryid <=1000 )
{
	$sql = "select publish,(select publish from category where id = $fid) as f_publish from category where id = $categoryid";
	$publish_ary = $db->get_one($sql);
	if($publish_ary["publish"] == 0 || $publish_ary["f_publish"] == 0)
		$showProductCategory = false;
}
//=================================================================//




$sql = "SELECT `type`,`name` FROM `inventory` WHERE `id` = '$inventoryid'";
$tmp_ary = $db->get_one($sql);
$inventory_type = $tmp_ary['type'];
$inventory_name = $tmp_ary['name'];
$sub_out_ary = array();


// =============== 1 start =============================
/*
$sql = "SELECT n.`id`, n.`name`, n.`pic`, n.`type`, n.`update` FROM `inventory` as n, `product_inventory` as i " . $sql_where . $sql_2 . $sql_3 . " GROUP BY n.`id` ORDER BY n.`seq` ASC";

//echo $sql;
$inventory_show_ary=array();
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $inventory_show_ary[] = $rs;
    if ($inventoryid == $rs['id']) 
	{
        $inventory_type = $rs['type'];
        $inventory_name = $rs['name'];
    }
}
*/

//2011 - 05 - 13 by martin
/*
if ($inventory_type == 2) 
{
} else {
    if (!is_numeric($categoryid) || $categoryid <=0) {
        header("Location:inventory.php");
    }
}
*/
///************check the category's publish state 	--- leo 2008-07-02 ********************/////

$c_publish = 1;
$f_publish = 1;

if($categoryid > 1000)
	$c_publish = 1;
else
{
	if( $categoryid!=""&&is_numeric($categoryid)&&  $categoryid > 0)
	{
		$sql_publish_check = "select publish,fid from category  where id = $categoryid";
		$query = $db->query($sql_publish_check);
		$rs_check = $db->fetch_array($query);
		$c_publish = $rs_check["publish"]; 	
	}
}


if ($fid == 0 || $categoryid > 1000 )
	$f_publish = 1;
else
{
	if($fid!="" && $fid > 0 && is_numeric($fid))
	{
		$sql_publish_check = "select publish as f_publish from category  where id = $fid";
		$query = $db->query($sql_publish_check);
		$rs_check = $db->fetch_array($query);
		$f_publish = $rs_check["f_publish"];	//parent category's publish state 
	}
}




// =============== 1 end ===============================
// =============== 2 start =============================

/*
$delete_flag_str = '';

$sql = "SELECT `id` FROM `product` WHERE `delete_flag` = 1";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $delete_flag_ary[] = $rs['id'];   
}
if (!empty($delete_flag_ary)) {
    $delete_flag_str = " AND c.`product_id` NOT IN (" . implode(",", $delete_flag_ary) . ") ";
}
*/

// =============== 2 end ===============================

// =============== 3 start =============================

//同种类category 里面的产品
$have_qty_product_ary = array();

$sql = "SELECT c.`category_id`, c.`product_id`, i.`qty` FROM `product_category` c,`product_inventory` i  WHERE i.`product_id`=c.`product_id` AND i.`qty`<>0 AND i.`inventory_id` = '$inventoryid'  ORDER BY c.`seq` ASC";

$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $category_id_ary[] = $rs['category_id'];   
    $have_qty_product_ary[] = $rs['product_id'];
    $product_qty_ary[$rs['product_id']] = $rs['qty'];
}

$have_qty_product_ary = array_unique($have_qty_product_ary);
$has=array();
foreach($have_qty_product_ary as $v)
{
 $has[]=$v;
}

$nowN=array_search($pid,$has);
//echo array_search('3225',$have_qty_product_ary);
$cun=count($has);
$Prv=($nowN-1)<0?0:($nowN-1);
$Next = ($nowN+1)>($cun-1)?0:($nowN+1);
//print_r($has);

$urlgotoN='inventory_details.php?names='.$names.'&inventoryid='.$inventoryid.'&categoryid='.$categoryid.'&pid='.$has[$Next].'&fid='.$fid;

$urlgotoP='inventory_details.php?names='.$names.'&inventoryid='.$inventoryid.'&categoryid='.$categoryid.'&pid='.$has[$Prv].'&fid='.$fid;


$have_qty_product_str = implode(",", $have_qty_product_ary);

$category_id_ary = array_unique($category_id_ary);
$have_set_category_id_str  = $category_id_str = implode(",", $category_id_ary);



$sql = "SELECT `id`, `name`, `pic`, `pic_2`, `seq`, `fid`, `description` FROM `category` WHERE `fid`>0 AND publish=1 AND `id` IN (" . $category_id_str . ")";
$query = $db->query($sql);
$sub_category_ary[] = $categoryid;
while($rs = $db->fetch_array($query)) {
    $category_id_ary[] = $rs['fid'];
    if ($rs['fid'] == $categoryid) {
        $sub_category_ary[] = $rs['id'];
    }

}

//$category_id_ary = array_unique($category_id_ary);
//$have_set_category_id_str = implode(",", $category_id_ary);

/*
if (!is_numeric($pid) || $pid <= 0) {
    header("Location:product.php");
    exit;
}
*/
/*
if ($fid<=0 && $categoryid<=0) 
{
    $sql_6 = '';
} else {
    $sql_6 = " AND `fid` IN (0,$cate_id)";
}

$sql = "SELECT `id`, `fid`, `name`, `pic`, `pic_2`, `seq`, `description` FROM `category` WHERE `id` IN (" . $have_set_category_id_str . ") AND publish=1 " . $sql_6 . " ORDER BY `seq` ASC";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    if ($rs['fid'] == 0) {
        $out_ary[] = $rs;
        if ($categoryid == $rs['id']) {
            $category_name = $rs['name'];
            $category_pic_2 = $rs['pic_2'];
        }
    } else {
        $sub_out_ary[] = $rs;
        $categoryid_str .= ',' . $rs['id'];
        if ($categoryid == $rs['id']) {
            $category_name = $rs['name'];
            $category_pic_2 = $rs['pic_2'];
        }
    }
}

$tmp_cate_str = implode(",", $sub_category_ary);
$no_preview_next = false;

if ($gotype == "preview" || $gotype == "next") {
    $pid_ary = array();
    $thisid = false;
    $new_pid = '';

    if ($jansen_type==2) {
        $sql_where = ' WHERE 1 = 1 ';
    } elseif ($jansen_type == 1) {
        $sql_where = ' WHERE `type` = 1 ';
    }


    $sql = "SELECT n.`id`, n.`name`, n.`pic`, n.`type` FROM `inventory` as n, `product_inventory` as i " . $sql_where . $sql_2 . $sql_3 . " GROUP BY n.`id` ORDER BY n.`seq` ASC";
	
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) 
	{
       
        if ($inventoryid == $rs['id'])
		{
            $this_type = $rs['type'];           
        }
    }

    if ($this_type == 2) {
        $sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3` FROM `product` p, `product_category` c, `category` ca WHERE ca.id=c.`category_id` AND p.`id`=c.`product_id` AND p.`id` IN (" . $have_qty_product_str . ") GROUP BY p.`id` ORDER BY ca.fid asc, ca.seq asc, p.`code` DESC, p.`id` DESC";
    } else {
        if ($fid<=0 && $categoryid<=0) 
		{
            $sql_4 = '';
        } else {
            $sql_4 = " AND p.`id` IN (" . $have_qty_product_str . ")";
        }

        if ($fid <= 0 && $categoryid <=0) {
            $sql_3 = "";
        } elseif ($fid <= 0) {
            $sql_3 = " AND c.`category_id` IN (" . $tmp_cate_str . ")";
        } else {
            $sql_3 = " AND c.`category_id`='$categoryid'";
        }
        $sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3` FROM `product` p, `product_category` c, `category` ca WHERE ca.`id`=c.`category_id` AND p.`id`=c.`product_id` " . $sql_3 . " " . $sql_4 . " GROUP BY p.`id` ORDER BY ca.`fid` asc, ca.`seq` asc, p.`code` DESC, p.`id` DESC";
    }

    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        if ($new_pid == '') {
            if ($gotype == "next" && $thisid) {
                $new_pid = $rs['id'];
            }
            if ($rs['id'] == $pid) {
                $thisid = true;
                if ($gotype == "preview") {
                    $new_pid = empty($pid_ary) ? $pid : array_pop($pid_ary);
                }
            }
            $pid_ary[] = $rs['id'];          
        }
    }

    if ($new_pid == "") {
        $new_pid = $pid;
    }
    $pid = $new_pid;

}
*/
///************check the category's publish state 	--- leo 2008-07-02 ********************/////

if (!empty($categoryid)) 
{
    browseTracking(4,$pid,$inventoryid);
} else {
    browseTracking(4,$pid,$inventoryid . '||' . $categoryid);
}



//################################################ one product details##################################


$sql = "SELECT * FROM `product` WHERE `id`='$pid'";
$product_info_ary = $db->get_one($sql);
$clearance_flag = false;

$item_flag = '';
if ($product_info_ary['new_flag'] == 1 && $clearance_flag) {
    $item_flag = "<font color='red' size='2'><em>New! Clearance!</em></font>";
} elseif ($product_info_ary['new_flag'] == 1) {
    $item_flag = "<font color='red' size='2'><em>New!</em></font>";
} elseif ($clearance_flag) {
    $item_flag = "<font color='red' size='2'><em>Clearance!</em></font>";
}


if (trim($product_info_ary['pic']) == "" || !file_exists("./zh/attachments/" . $product_info_ary['pic'])) {
    $product_info_ary['pic'] = 'no_big.gif';
}

$product_info_ary['width'] = sprintf("%0.2f", $product_info_ary['width']);
$product_info_ary['height'] = sprintf("%0.2f", $product_info_ary['height']);
$product_info_ary['length'] = sprintf("%0.2f", $product_info_ary['length']);

$width_inch = sprintf("%0.2f", $product_info_ary['width2']);
$height_inch = sprintf("%0.2f", $product_info_ary['height2']);
$length_inch = sprintf("%0.2f", $product_info_ary['length2']);

$product_info_ary['cbm'] = sprintf("%0.2f", $product_info_ary['cbm']);
$product_info_ary['cuft'] = sprintf("%0.2f", $product_info_ary['cuft']);

$product_info_ary['price_1'] = number_format($product_info_ary['price_1'], 2, '.', ',');
$product_info_ary['price_2'] = number_format($product_info_ary['price_2'], 2, '.', ',');
$product_info_ary['price_3'] = number_format($product_info_ary['price_3'], 2, '.', ',');



$sql = "SELECT `product_id`, `qty`, `special_price`, `special_price2` FROM `product_inventory` WHERE `product_id` ='$pid' AND `inventory_id`='$inventoryid'";
$tmp_ary = $db->get_one($sql);
$sp = $tmp_ary['special_price'];
$sp2 = $tmp_ary['special_price2'];
$qty = $tmp_ary['qty'];
$code = $product_info_ary['code'];
$moreqty=$qty;

if ($jansen_type==2) {
    $sp = number_format($sp, 2, '.', ',');
    $money_type = 'EUR';
    if ($sp == 0) 
	{
        $sp_div = '';
    } else {
        $sp = number_format($sp, 2, '.', ',');        
        $sp_div = '<tr>
                            <td height="15" colspan=1 align="right" valign="middle"><strong>Special</strong> :</td>
                            <td align="right">' . $sp . '</td>
                            <td align="right">&nbsp;&nbsp;' . $money_type . '</td>
                            <td colspan=5 align="right"></td>
                          </tr>
	                   ';

    }
    if ($inventory_type == 2) {
        $price_show = '<tr>
                            <td height="15" colspan=1 align="right" valign="middle"><strong>Regular</strong> :</td>
                            <td align="right">' . $product_info_ary['price_3'] . '</td>
                            <td align="right">&nbsp;&nbsp;' . $money_type . '</td>
                            <td colspan=5 align="right"></td>
                          </tr>
	                   ' . $sp_div;
    } else {
        $price_show = '<tr>
                            <td height="15" colspan=1 align="right" valign="middle"><strong>Regular</strong> :</td>
                            <td align="right">' . $product_info_ary['price_3'] . '</td>
                            <td align="right">&nbsp;&nbsp;' . $money_type . '</td>
                            <td colspan=5 align="right"></td>
                          </tr>
	                   ' . $sp_div;
    }

} elseif ($jansen_type == 1) {
    $sp = number_format($sp2, 2, '.', ',');
    $money_type = 'EUR';

    if ($sp == 0) {
        $sp_div = '';
    } else {
        $sp = number_format($sp, 2, '.', ',');
        $sp_div = '<tr>
                            <td height="15" colspan=1 align="right" valign="middle"><strong>Special</strong> :</td>                            
                            <td align="right">' . $sp . '</td>
                            <td align="left">&nbsp;&nbsp;' . $money_type . '</td>
                            <td colspan=5 align="right"></td>
                          </tr>
	                   ';

    }

    $price_show = '<tr><td height="15" colspan=1 align="right" valign="middle"><strong>Price</strong> :</td>
                            <td align="right">' . $product_info_ary['price_3'] . '</td>
                            <td align="left">&nbsp;&nbsp;' . $money_type . '</td>
                            <td colspan=5 align="right"></td>
                          </tr>
	                   ' . $sp_div;
}

if($categoryid>0 && $inventoryid > 0) {
    $return_href = "inventory_list.php?names=".$names."&inventoryid=$inventoryid&fid=$fid&categoryid=$categoryid";
} else {
    $return_href = "javascript:history.back(1)";
}

$postiondiv='<div class="position" ><a href="index.php">Home</a> &gt; <a href="inventory.php">Inventory Category</a> &gt; <a href="inventory_category.php?names='.$names.'&inventoryid='.$inventoryid.'"> '.$names.'</a> &gt; <a href="inventory_list.php?names='.$names.'&inventoryid='.$inventoryid.'&categoryid='.$categoryid.'">Inventory List</a> &gt; <span>'.$product_info_ary['code'].'</span> </div>';

$baner_img='<div class="bigtitle">Inventory</div>';

require_once PrintEot('header');
require_once PrintEot('inventory_details');
require_once PrintEot('footer');

?>